<template>
  <div id="move" v-loading="loading">
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" :title="$t('ex.move')" @allocation="allocation"></v-tableTitle>
    <v-table :checked="false" :pageSize="pageSize" :page="page" :tableConfig="tableConfig" :tableData="tableData" :total="total" @findData="getTableData" @getTableData="getTableData"
      @searhData="searhData" @selectData="selectData" @setdialog="setdialog" @upData="getTableData" @setEditData="setEditData" ref="table"></v-table>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
    name: "move",
    mixins: [baseMixin, config],
    data() {
        return {
            loading: false,
            tableConfig: [
                {
                    label: this.$t('config.shipper'),
                    prop: "storerkey",
                    input: "input",
                },
                {
                    label: this.$t('config.sku'),
                    prop: "sku",
                    input: "input"
                },
                {
                    label: this.$t('config.loc'),
                    prop: "fromloc",
                    input: "input",
                },
                {
                    label: this.$t('config.toLoc'),
                    prop: "toloc",
                    eidtFlag: true,
                    verify(value, that) {
                        value = value.replace(/\s*/g, "");
                        if (value === "") {
                            that.$message.error(this.$tj('config.loc','app.emptyMes'));
                            return false
                        } else {
                            return that.$api.get(`/queryLocCount/${value}`).then(res => {
                                
                                    if (parseInt(res.bean) > 0) {
                                        return true;
                                    } else {
                                        that.$message.error(this.$t('msg.msg23') + value);
                                        return false
                                    }
                            }).catch(e => {
                                that.$message.error(e);
                                return false;
                            })
                        }
                    }
                },
                {
                    label: this.$t('bill.lot'),
                    prop: "lot",
                    input: "input",
                },
                {
                    label: this.$t('in.lpn'),
                    prop: "fromid",
                    input: "input",
                },
                {
                    label: this.$t('in.toid'),
                    prop: "toid",
                    eidtFlag: true
                },
                {
                    label: this.$t('common.status'),
                    prop: "status",
                    input: "input",
                },
                {
                    label: this.$t('config.num'),
                    prop: "qty",
                    input: "input",
                },
                {
                    label: this.$t('out.moveqty'),
                    prop: "moveqty",
                    eidtFlag: true,
                    verify(value, that) {
                        if (value === '') {
                            that.$message.error(this.$tj('out.moveqty','app.emptyMes'));
                            return false;
                        } else {
                            var re = /^[0-9]*$/;
                            let te = value;
                            let flag = re.test(te)
                            if (flag) {
                                if (value < 0) {
                                    that.$message.error(this.$t('app.inputGreaterZeroNumberPlease'));
                                    return false;
                                } else {
                                    return true;
                                }
                            } else {
                                that.$message.error(this.$t('app.inputGreaterZeroNumberPlease'));
                                return false;
                            }
                        }
                    },
                },
                {
                    label: this.$t('outbound.qtyallocated'),
                    prop: "qtyallocated",
                    input: "input",
                },
                {
                    label: this.$t('out.qtyavailqty'),
                    prop: "qtyavailqty",
                    input: "input",
                },
                {
                    label: this.$t('config.batchProp',{no:'01'}),
                    prop: "lottable01",
                    input: "input",
                },
                {
                    label: this.$t('config.batchProp',{no:'02'}),
                    prop: "lottable02",
                    input: "input",
                },
                {
                    label: this.$t('config.batchProp',{no:'03'}),
                    prop: "lottable03",
                    input: "input",
                },
                {
                    label: this.$t('in.lottable04'),
                    prop: "lottable04",
                    input: "date",
                },
                {
                    label: this.$t('in.lottable05'),
                    prop: "lottable05",
                    input: "date",
                },
                {
                    label: this.$t('config.batchProp',{no:'06'}),
                    prop: "lottable06",
                    input: "input",
                },
                {
                    label: this.$t('config.batchProp',{no:'07'}),
                    prop: "lottable07",
                    input: "input",
                },
                {
                    label: this.$t('config.batchProp',{no:'08'}),
                    prop: "lottable08",
                    input: "input",
                },
                {
                    label: this.$t('config.batchProp',{no:'09'}),
                    prop: "lottable09",
                    input: "input",
                },
                {
                    label: this.$t('config.batchProp',{no:'10'}),
                    prop: "lottable10",
                    input: "input",
                },
                {
                    label: this.$t('ex.lottable11'),
                    prop: "lottable11",
                    input: "date",
                },
                {
                    label: this.$t('in.lottable12'),
                    prop: "lottable12",
                    input: "date",
                }
            ],
            tableData: [],
          page: 1,
            pageSize: 10,
            total: 0,
            tableSelectData: [],
            tableSearhData: {},
            eidtData: [],
            eidtFlag: true,
        };
    },
    computed: {
        btnConfig() {
            return [
                {label: this.$t('ex.batchallocation'), certigier: this.eidtFlag||!this.btnPermNameList.includes('WM06020301'), invalid: "allocation"},
            ]
        }
    },
    created() {
    },
    methods: {
        setEditData(data) {
            let index;
            let flag = this.eidtData.some((item, idx) => {
                index = idx;
                return item.serialkey === data.serialkey;
            })
            if (flag) {
                this.eidtData[index] = data;
            } else {
                this.eidtData.push(data);
            }
        },
        //选中的内容
        selectData(data) {
            this.tableSelectData = data;
        },
        async allocation(data) {
            let type = "";
            let flag = this.eidtData.every(item => {
                item.toloc = item.toloc.replace(/\s*/g, "");
                if (item.toloc === "") {
                    type = "toloc";
                }
                if (item.moveqty <= 0) {
                    type = "moveqty"
                }
                return item.toloc !== "" && (item.moveqty !== "" && item.moveqty > 0);
            })
            if (flag) {
                try {
                    let res = await this.$api.post(`/excuteMoveList`, this.eidtData);
                    
                        this.$message.success(res.returnMgr);
                        let page = sessionStorage.getItem('page');
                      this.eidtData = [];
                      this.tableData = [];
                        this.getTableData(page, this.tableSearhData);

                    
                } catch (e) {
                    this.$message.error(e);
                }
            } else {
                if (type === "moveqty") {
                    this.$message.error(this.$t('msg.msg25'));
                } else {
                    this.$message.error(this.$t('msg.msg26'));
                }

            }
        },
        async getTableData(page, data) {
            try {
                this.loading = true;
                let res = await this.$api.post(`/queryLotxlocidListByMove/${page}`, data);
                
                    this.eidtData = []
                    this.tableData = res.page.list;
                  this.page = res.page.curPage;
                    this.pageSize = res.page.pageSize;
                    this.total = res.page.total;
                
            } catch (e) {
                this.$message.error(e);
            } finally {
                this.loading = false;
            }
        },
        //搜索的值
        searhData(data, up) {
            if (!up) {
                this.tableSearhData = data;
            } else {
                this.tableSearhData = data;
                this.getTableData(1, this.tableSearhData);
            }
        },
        //设置弹框
        setdialog(data) {
            this.$store.commit("setRouteData", data);
            this.$router.push({name: 'moveDeawer'});
        },
    },
    mounted() {
        this.$store.commit("setSkip", {});
        this.$store.commit("setNewFlag", false);
        this.$store.commit("setRouteData", {});
        this.getTableData(1, {});
    },
    watch: {
        eidtData(data) {
            if (data.length > 0) {
                this.eidtFlag = false;
            } else {
                this.eidtFlag = true;
            }
        }
    },
    components: {
        "v-tableTitle": tableTitle,
        "v-table": table,
    }
};
</script>

<style scoped>
#move {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
